import speech_recognition as sr
import os

def transcribe_audio(audio_path):
    # 创建一个Recognizer对象
    recognizer = sr.Recognizer()

    # 读取音频文件
    with sr.AudioFile(audio_path) as source:
        # 读取音频数据
        audio_data = recognizer.record(source)
        print("录音已读取")

        # 使用Google Web Speech API进行语音识别
        try:
            text = recognizer.recognize_google(audio_data, language='zh-CN')
            print("识别结果:", text)
            return text
        except sr.UnknownValueError:
            print("Google Web Speech API无法识别音频")
            return None
        except sr.RequestError as e:
            print(f"请求Google Web Speech API时出错: {e}")
            return None

# 调用函数并传入音频文件路径
audio_path = './input.wav'
transcribed_text = transcribe_audio(audio_path)

if __name__ == '__main__':
    print(transcribed_text)
